﻿#include "stdafx.h"
#include "QMyMessageBox.h"
#include "QIconHelper.hpp"

#if defined(_MSC_VER) && (_MSC_VER >= 1600)  
# pragma execution_character_set("utf-8")  
#endif 


QMyMessageBox::QMyMessageBox(QWidget *parent)
	: QDialog(parent)
	, m_bMaximized(false)
	, m_bMoveable(false)
{
	ui.setupUi(this);
	setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
	setWindowModality(Qt::WindowModal);

	QIconHelper::SetIcon(ui.menuButton_Close, QChar(0xf00d), 10);

	connect(ui.menuButton_Close, SIGNAL(clicked()), this, SLOT(close()));
	connect(ui.btnCancel, SIGNAL(clicked()), this, SLOT(close()));
	connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(sltOk()));

}

QMyMessageBox::~QMyMessageBox()
{

}


void QMyMessageBox::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		dragPosition = event->globalPos() - frameGeometry().topLeft();

		QRect rect = ui.widget_title->rect();
		if (rect.contains(event->pos()))
		{
			m_bMoveable = true;
		}
		event->accept();
	}
}

void QMyMessageBox::mouseMoveEvent(QMouseEvent *event)
{
	if (event->buttons() & Qt::LeftButton && !m_bMaximized && m_bMoveable)
	{
		move(event->globalPos() - dragPosition);	
	}
}

void QMyMessageBox::mouseReleaseEvent(QMouseEvent *event)
{
	if (m_bMoveable)
	{
		m_bMoveable = false;
	}
}

void QMyMessageBox::SetMessage(const QString &msg, int type)
{
	if (type == 0) 
	{
		ui.label_MessageType->setProperty("MessageType", "information");

		// 手动更新样式
		ui.label_MessageType->style()->unpolish(ui.label_MessageType);
		ui.label_MessageType->style()->polish(ui.label_MessageType);
		ui.label_MessageType->update();

		ui.btnCancel->setVisible(false);
		ui.label_title->setText(QStringLiteral("提示"));
	} 
	else if (type == 1) 
	{
		ui.label_MessageType->setProperty("MessageType", "question");

		// 手动更新样式
		ui.label_MessageType->style()->unpolish(ui.label_MessageType);
		ui.label_MessageType->style()->polish(ui.label_MessageType);

		ui.label_title->setText(QStringLiteral("询问"));
	} 
	else if (type == 2) 
	{
		ui.label_MessageType->setProperty("MessageType", "error");
		// 手动更新样式
		ui.label_MessageType->style()->unpolish(ui.label_MessageType);
		ui.label_MessageType->style()->polish(ui.label_MessageType);

		ui.btnCancel->setVisible(false);
		ui.label_title->setText(QStringLiteral("错误"));
	}
	ui.labInfo->setText(msg);
}

void QMyMessageBox::sltOk()
{
	done(1);
	close();
}

void QMyMessageBox::information(QWidget * parent, QString info)
{
	QMyMessageBox msg(parent);
	msg.SetMessage(info, 0);
	msg.exec();
}

//显示错误框,仅确定按钮
void QMyMessageBox::error(QWidget * parent, QString info)
{
	QMyMessageBox msg(parent);
	msg.SetMessage(info, 2);
	msg.exec();
}

//显示询问框,确定和取消按钮
int QMyMessageBox::question(QWidget * parent, QString info)
{
	QMyMessageBox msg(parent);
	msg.SetMessage(info, 1);
	return msg.exec();
}
